package com.example.my.android;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

public class StudentInfoActivity extends AppCompatActivity {

    private EditText codeEdit;
    private EditText nameEdit;
    private EditText ageEdit;
    private RadioButton maleRadio;
    private RadioButton femaleRadio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_info);

        codeEdit = findViewById(R.id.code_edit);
        nameEdit = findViewById(R.id.name_edit);
        ageEdit = findViewById(R.id.age_edit);
        maleRadio = findViewById(R.id.male_radio);
        femaleRadio = findViewById(R.id.female_radio);
        findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = codeEdit.getText().toString();
                String name = nameEdit.getText().toString();
                int age = Integer.valueOf(ageEdit.getText().toString());
                String gender = maleRadio.isChecked()?"男":"女";

                ContentValues values = new ContentValues();
                values.put("code", code);
                values.put("name", name);
                values.put("age", age);
                values.put("gender", gender);

                MyDb myDb = new MyDb(getApplicationContext());
                SQLiteDatabase db = myDb.getWritableDatabase();
                long rows = db.insert("students", null, values);

                Intent intent = new Intent();
                intent.putExtra("success", rows>=1);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}